package com.googlecode.gaal.analysis.impl;

public class Stopwatch {
    private long startTime;
    private long stopTime;
    private boolean isRunning;

    public void start() {
        if (isRunning) {
            throw new IllegalStateException("is already running");
        }
        startTime = System.currentTimeMillis();
        isRunning = true;
    }

    public void stop() {
        if (!isRunning) {
            throw new IllegalStateException("not running");
        }
        stopTime = System.currentTimeMillis();
        isRunning = false;
    }

    public void reset() {
        if (!isRunning) {
            throw new IllegalStateException("not running");
        }
        startTime = System.currentTimeMillis();
    }

    @Override
    public String toString() {
        if (isRunning) {
            stopTime = System.currentTimeMillis();
        }
        return String.format("%d ms", stopTime - startTime);
    }

}
